<?php
$settings = parse_ini_file("settings.ini", true, INI_SCANNER_RAW);
$server_mysql = "tloyd-ser";	// erase this after conversion over to $settings global variables

include_once("inc/ClientManager.php");
include_once("inc/TaskManager.php");
include_once("inc/ParamManager.php");
include_once("inc/Network.php");
include_once("inc/db.php");

db_init();

// global variables
$css_table_id = "box-table-a";
$network = new Network();
$pm = new ParamManager();

// load up available nodes using the directory names within 'nodes'
$nodes = array();
$d_nodes = opendir("nodes");
if($d_nodes != null) {
	while(false !== ($entry = readdir($d_nodes))) {
		if(is_dir("nodes\\$entry")) {
			if(!strcmp($entry, ".")) {
				// ignore directory "."
			}
			elseif(!strcmp($entry, "..")) {
				// ignore directory ".."
			}
			else {
				// remember the node
				$nodes[$entry] = $entry;
			}
		}
	}
}

// use the name of each node to load up the settings .ini file
foreach($nodes as $key => $value) {
	$nodes[$key] = parse_ini_file("nodes/$key/$key.ini", false);
}

// Load up task information from the server
$tm = new TaskManager($network);
// Load up client information from the server
$cm = new ClientManager($network);

// process passed in data
if(isset($_POST["cmd"])) {
	foreach($nodes as $key => $value) {
		if(isset($nodes[$key]["process"])) {
			include "nodes/$key/". $nodes[$key]["process"];
		}
	}
}

// Work with cookies
if(isset($_POST["cmd"]) && !strcmp($_POST["cmd"], "curr_wb")) {
	$curr_wb = $_POST["detail"];
}
else if(isset($_COOKIE["curr_wb"])) {
	$curr_wb = $_COOKIE["curr_wb"];
}
else {
	$curr_wb = "main";
}

?>
<html>
<head>
<link href='style.css' rel='stylesheet' type='text/css'>

<script type="text/javascript">
function process(cmd, detail) {
//	alert("process(" + cmd + ", " + detail + ")");
	document.main.cmd.value = cmd;
	document.main.detail.value = detail;
	document.main.submit();
}

function toggle_hide(div_id) {
	var item = document.getElementById(div_id);
	if(item.style.display == "inline-block") {
		item.style.display = "none";
	}
	else {
		item.style.display = "inline-block";
	}
}
</script>

</head>

<body topmargin='0' leftmargin='0'>

<form name='main' method='post'>
<input type='hidden' name='cmd' value=''/>
<input type='hidden' name='detail' value=''/>
</form>

<table cellpadding='5' cellspacing='0' border='1'>
<tr bgcolor="#f5da81">
<td width='100%'>stbd, welcome
<!-- <td align='right' nowrap>stbd, welcome tloyd -->
<td align='center' colspan='1'>Network
<td align='center' colspan='3'>Tasks
<tr bgcolor="#f5da81">
<td nowrap>
<?php
echo "<select name='priority' onChange='javascript:process("
	. "\"curr_wb\", "
	." this.options[this.selectedIndex].value"
	. ")'>\n";
//	. ")' style=\"width: 100%\">\n";

foreach($nodes as $key => $value) {
	if(!strcmp($key, $curr_wb)) {
		echo "<option value='$key' selected>". $nodes[$key]["show"] ."\n";
	}
	else {
		echo "<option value='$key'>". $nodes[$key]["show"] ."\n";
	}
}

echo "</select>\n";

$clients = 0;
foreach($cm->clients as $client) {
	$clients ++;
}

$pending = $active = $complete = 0;
foreach($tm->tasks as $task) {
	switch($task->state) {
		case 0: $pending ++;
				break;
		case 1: $active ++;
				break;
		case 2: $complete ++;
				break;
	}
}

//echo "<td>&nbsp;";
echo "<td nowrap>Clients: $clients\n";
echo "<td nowrap>Pending: $pending\n";
echo "<td nowrap>Active: $active\n";
echo "<td nowrap>Complete: $complete\n";
?>
</table>
<?php
foreach($nodes as $key => $value) {
	if(!strcmp($key, $curr_wb)) {
		include "nodes/$key/". $nodes[$key]["index"];
	}
}
?>

</body>
</html>
